import { IContextMenu } from '@amcax/base';
import { nextmeshEventCenter } from '@app-nextmesh/common/events/EventCenter';
import { nmAppErrorLog, nmAppLog } from '@app-nextmesh/common/utils';
import { documentV2 } from '@app-nextmesh/domain';
import { updateGeometriesVisible } from '@app-nextmesh/utils';
import { deleteModels } from '@app-nextmesh/utils/renderModel';
import { Item, Menu, Separator } from 'react-contexify';

function Component({ id }: { id: string }) {
  return (
    <Menu id={id}>
      <Item
        id='hide'
        onClick={({ props }) => {
          updateGeometriesVisible('hide', props);
        }}>
        隐藏
      </Item>
      <Item
        id='show'
        onClick={({ props }) => {
          updateGeometriesVisible('show', props);
        }}>
        显示
      </Item>
      <Separator />
      <Item
        id='delete'
        onClick={async ({ props }) => {
          nmAppLog('delete', { props });
          try {
            if (nextmeshEventCenter().pickMode === 'geometry') {
              // TODO delet geometry
            } else {
              await documentV2.deleteMesh(props);
              deleteModels(props);
            }
          } catch (error) {
            nmAppErrorLog('删除模型/网格失败', error);
          }
        }}>
        删除
      </Item>
    </Menu>
  );
}
const show = (): boolean => {
  return true;
};
/**
 * 选中模型后的右键菜单
 */
export const NextMeshSelectedContextMenu: IContextMenu = {
  id: 'nextmeshSelectedContextMenu',
  show,
  render: (id: string) => (
    <Component
      id={id}
      key={`nextmesh-selected-context-menu-${id}`}
    />
  ),
};
